import pathlib


def traverse_dir_files(root_dir, ext=None):
    """
    列出文件夹中的文件, 深度遍历
    :param root_dir: 根目录
    :param ext: 后缀名
    :param is_sorted: 是否排序，耗时较长
    :return: [文件路径列表, 文件名称列表]
    """
    names_list = []
    paths_list = []
    for path in list(pathlib.Path(root_dir).rglob("*")):
        path = str(path)
        name = path.split("/")[-1]
        if name.startswith('.') or "." not in name:  # 去除隐藏文件
            continue
        if ext:  # 根据后缀名搜索
            if name.endswith(ext):
                names_list.append(name)
                paths_list.append(path)
        else:
            names_list.append(name)
            paths_list.append(path)
    if not names_list:  # 文件夹为空
        return paths_list, names_list

    return paths_list, names_list


a = traverse_dir_files(r"C:\Users\liyuzheng\androidProject\starmaker-android-client", "build.gradle")


def read_gradle_file(path):
    with open(path, encoding='utf-8') as f:
        while True:
            line = f.readline()
            if line:
                temp = line.strip()
                if temp.startswith('implementation project'):
                    build_setting(temp)
            else:
                break


# libraryA(
#             useByAar: false,
#             groupId: 'com.james',
#             artifactId: 'libraryA',
#             version: '1.9.5',
#     )

module_list = []


def build_setting(str):
    start = int(str.find("'") + 1)
    end = int(len(str) - 2)
    str1 = str[start:end]
    str2 = str1[str1.rfind(":") + 1:len(str1)]
    module_name = str2.replace("_", "")
    if module_list.__contains__(str2):
        pass
    else:
        module_list.append(str2)
        final = module_name + "(" + "\n" + "\tuseByAar: false,\n" + "\tgroupId: 'com.ushowmedia.starmaker',\n" + "\tartifactId:'" + str2 + "',\n" + "\tversion:'1.0.0'\n) "
        print(final)


for r in a[0]:
    read_gradle_file(r)
























